package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hxf.domain.bo.TypeBo;
import com.ruoyi.hxf.domain.vo.TypeVo;
import com.ruoyi.hxf.domain.Type;
import com.ruoyi.hxf.mapper.TypeMapper;
import com.ruoyi.hxf.service.ITypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 班级创建管理Service业务层处理
 *
 * @author hxf
 * @date 2022-12-15
 */
@RequiredArgsConstructor
@Service
public class TypeServiceImpl implements ITypeService {

    private final TypeMapper baseMapper;

    /**
     * 查询班级创建管理
     */
    @Override
    public TypeVo queryById(Long typeId){
        return baseMapper.selectVoById(typeId);
    }


    /**
     * 查询班级创建管理列表
     */
    @Override
    public List<TypeVo> queryList(TypeBo bo) {
        LambdaQueryWrapper<Type> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Type> buildQueryWrapper(TypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Type> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), Type::getTypeName, bo.getTypeName());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Type::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Type::getStatus, bo.getStatus());
        lqw.eq(bo.getParentId()!=null, Type::getParentId, bo.getParentId());
        lqw.eq(bo.getDeptId()!=null, Type::getDeptId, bo.getDeptId());
        lqw.eq(Type::getIsDelete,0);
        lqw.orderByAsc(Type::getParentId)
            .orderByAsc(Type::getDeptId);
        return lqw;
    }

    /**
     * 新增班级创建管理
     */
    @Override
    public Boolean insertByBo(TypeBo bo) {
        Type add = BeanUtil.toBean(bo, Type.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeId(add.getTypeId());
        }
        return flag;
    }

    /**
     * 修改班级创建管理
     */
    @Override
    public Boolean updateByBo(TypeBo bo) {
        Type update = BeanUtil.toBean(bo, Type.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Type entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除班级创建管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            TypeVo typeVo = baseMapper.selectVoById(id);
            typeVo.setIsDelete("1");
            Type update = BeanUtil.toBean(typeVo, Type.class);
            baseMapper.updateById(update);
        }
        return true;
    }

    @Override
    public int updateByType(Type type) {
        return baseMapper.updateByType(type);
    }

    @Override
    public Long selectTypeByDeptId(Long classId) {
        return baseMapper.selectTypeByDeptId(classId);
    }

    @Override
    public void updateUserRole(Long oldClassLeader,String s) {
        baseMapper.updateUserRole(oldClassLeader,s);
    }

    @Override
    public String selectRoleName() {
        return baseMapper.selectRoleName();
    }

    @Override
    public void insertUserRole(Long aLong, String s) {
        baseMapper.insertUserRole(aLong,s);
    }

    @Override
    public String selectFromClass(Long userId) {
        return  baseMapper.selectFromClass(userId);

    }

    @Override
    public String selectPasswordByDeptId(Long classId) {
        return  baseMapper.selectPasswordByDeptId(classId);
    }
    /**
     * 根据班级Id插入家长Id
     * @param classId 班级Id
     * @return
     */
    @Override
    public String selectClassParentByDeptId(Long classId) {
        return baseMapper.selectClassParentByDeptId(classId);
    }

    @Override
    public void updateClassParent(String classParent,Long classId) {
        baseMapper.updateClassParent(classParent,classId);
    }
    /**
     * 修改班级密码
     * @param classId
     * @param classPassword
     * @return
     */
    @Override
    public int updateClassPassword(String classId, String classPassword) {
        return baseMapper.updateClassPassword(classId,classPassword);
    }

    @Override
    public Type selectByDeptId(Long deptId) {
        return baseMapper.selectByDeptId(deptId);
    }
}
